﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface IUserInDepartmentsDAL : IDAL<UserInDepartments>
    {
        List<UserInDepartments> GetUsersByDepartmentId(Guid Did);
        List<UserInDepartments> GetUsersByDepartmentId(Func<UserInDepartments, bool> exp, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
    }

    public class UserInDepartmentsDAL : BaseDAL<UserInDepartments>, IUserInDepartmentsDAL
    {
        public UserInDepartmentsDAL(IUnitOfWork context)
            : base(context)
        {
        }

        public List<UserInDepartments> GetUsersByDepartmentId(Guid Did)
        {
            return dataContext.UserInDepartments.Include("Users").Where(t => t.DepartmentId == Did).ToList();             
        }

        public List<UserInDepartments> GetUsersByDepartmentId(Func<UserInDepartments, bool> exp, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = dataContext.UserInDepartments.Include("Users").Where(exp).AsQueryable();
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();
        }
    }
}
